using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyGene : MonoBehaviour
{
    public float xrange = 10f;
    public float yrange = 10f;
    public int enemyCool = 5;
    public GameObject Prefab;
    public EnemyCount EnemyCount;
      private int lastEnemyCount = 0;
     public float checkInterval = 1f; // 检查间隔
     private float timer = 0f;// 计时器
    void Update()
    {
         timer += Time.deltaTime;
        // 在检查间隔内进行检查
        if (timer >= checkInterval)
        {
            timer = 0f;

            // 检查条件是否满足
            if (IsEnemyCountMultiple() && PlayerHasKilledEnemies())
            {
                GenerateEnemy();
            }
        }
        
    }
    private bool IsEnemyCountMultiple()
    {
        int currentEnemyCount = EnemyCount.enemyCount;
        return currentEnemyCount > 0 && currentEnemyCount % enemyCool == 0;
    }
     private bool PlayerHasKilledEnemies()
    {
        int currentEnemyCount = EnemyCount.enemyCount;
        return currentEnemyCount > lastEnemyCount;
    }



     private void GenerateEnemy()
    {
        var pt = Camera.main.ViewportToWorldPoint(new Vector2(Random.Range(-xrange,xrange),Random.Range(-yrange,yrange)));
        var clone = Instantiate(Prefab);
        clone.transform.position = pt;
    }
}
